var app = getApp()
Page({
  data: {
    check_confirm_already:0,
    check_confirm_number:0,
    order_info:[{
      NO:"",
      vehicle:"",
      driver:"",
      total_package:"",
      total_cbm:"",
      total_weight:"",
      state:"",
      ADD_TIME:""
    }],
    order_list:[],
    userType:""
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--货品确认清单"});
    var that = this;
    wx.showToast({
      title: "数据加载中，请稍候...",
      icon: 'loading',
      duration: 3000
    });
    that.setData({
      userType:app.globalData.userType
    })
    wx.request({
        url: 'https://www.uamaker.com/wechat/wxapplet/getGoodsList',
        method:'GET',
        header: {
              'content-type': 'application/json',
              'Cookie':app.globalData.cookie
        },
        data: {
          id:e.id,
          openid:app.globalData.openid,
          userType:app.globalData.userType
        },
        success: function(res) {console.log(res.data)
          wx.hideToast();
          if(res.data.success){
            that.setData({
              hasData:true
            });
            var info_data = res.data.data[0],_info_data = [];
            for(var i=0;i < info_data.length; i++){
               _info_data.push({
                  NO:info_data[i].NO,
                  vehicle:info_data[i].plate,
                  driver:info_data[i].driver_name,
                  ADD_TIME:info_data[i].ADD_TIME,
                  total_package:info_data[i].total_package,
                  total_cbm:info_data[i].total_cbm,
                  total_weight:info_data[i].total_weight,
                  state:that.orderStateCode(info_data[i].state)
                });
            };
            that.setData({
              order_info:_info_data
            });
            var list_data = res.data.data[1],_list_data = [];
            for(var i=0;i < list_data.length; i++){
               _list_data.push({
                  pl_id:list_data[i].ID,
                  pl_name:list_data[i].name,
                  pl_unit:list_data[i].unit,
                  pl_size:list_data[i].size,
                  pl_quantity:list_data[i].quantity,
                  pl_weight:list_data[i].weight,
                  pl_cbm:list_data[i].cbm,
                  checkbox:"0",
                  checked:false,
                  pl_quantity_check:list_data[i].quantity,
                  pl_weight_check:list_data[i].weight,
                  pl_cbm_check:list_data[i].cbm,
                  quantity_check_result:"0",
                  weight_check_result:"0",
                  cbm_check_result:"0"
                });
            };
            that.setData({
              order_list:_list_data
            });
            that.setData({
              check_confirm_number:list_data.length
            });
            
          }else{
            that.setData({
              hasData:false
            })
          }
        },
        fail:function(error){
          wx.hideToast();
          console.log(error);
          that.setData({
              hasData:false
            })
        }
      })
  },
  checkboxChange: function (e) {
      var that = this;
      var id = e.target.id;
      var _id = id.split("_"); 
        var order_list = that.data.order_list, values = e.detail.value;
        for (var i = 0;i<order_list.length;i++) {
            if(order_list[i].pl_id == _id[1]){
              if(order_list[i].checkbox == "0"){
                  order_list[i].checkbox = "1";
                  order_list[i].checked = true;
                  that.setData({
                    check_confirm_already:that.data.check_confirm_already + 1
                  });
                  
              }else{
                  order_list[i].checkbox = "0";
                  order_list[i].checked = false;
                  that.setData({
                    check_confirm_already:that.data.check_confirm_already - 1
                  });
              }
            };
        };
        that.setData({
            order_list: order_list
        });
    },
    confirm_backtolist:function(e){
        if(this.data.check_confirm_already == this.data.check_confirm_number){
            app.globalData.goods_confirm = true;
            wx.navigateBack({
              delta: 1
            });
        }else{
            wx.showModal({
              title: '温馨提示',
              content: '尚有货品没有确认！',
              showCancel: false,
              success: function(res) {
              }
            })
        }
    },
    checkNumber:function(e){
        console.log(e)
        var that = this;
        var id = e.target.id;
        var _id = id.split("_"); 
        var order_list = that.data.order_list, values = e.detail.value;
        for (var i = 0;i<order_list.length;i++) {
            if(order_list[i].pl_id == _id[1]){
                var check_result = values - order_list[i]["pl_"+_id[0]];
                if(check_result != 0){
                   order_list[i][_id[0]+"_check_result"] = check_result;
                   order_list[i]["pl_"+_id[0]+"_check"] = e.detail.value;
                }
            };
        };console.log(order_list)
        that.setData({
            order_list: order_list
        });
    },
    orderStateCode:function(code){
      //状态(0:待接单，1:待装货，2：司机确认装货,3:门店待签收,4:司机确认签收,5:配送结束)
      switch(code){
          case "0":
            return "待司机接单";
            break;
          case "1":
            return "待配送装货";
            break;
          case "2":
            return "待司机确认装货";
            break;
          case "3":
            return "待门店签收";
            break;
          case "4":
            return "待司机确认签收";
            break;
          case "5":
            return "配送结束";
            break;
          default:
            break;
      }
    }
})
